﻿// <copyright file="PointsVisual.cs" company="Oleg V. Polikarpotchkin">
// Copyright © 2008-2009 Oleg V. Polikarpotchkin. All Right Reserved
// </copyright>
// <author>Oleg V. Polikarpotchkin</author>
// <email>ov-p@yandex.ru</email>
// <date>2009-02-09</date>
// <summary>OpenWPFChart library. Points Visual draws the set of points.</summary>
// <revision>$Id$</revision>

using System.Diagnostics;
using System.Windows;
using System.Windows.Media;

namespace OpenWPFChart.Parts
{
	/// <summary>
	/// Points Visual draws the set of points.
	/// </summary>
	public class PointsVisual : ItemVisual
	{
		/// <summary>
		/// Renders the points.
		/// </summary>
		protected internal override void Render()
		{
			Children.Clear();

			using (DrawingContext dc = RenderOpen())
			{
				ScatteredPointsDataView dataView = DataView as ScatteredPointsDataView;
				if (dataView == null)
					return;
				IPoint2DSeries pointSeries = dataView.ItemData as IPoint2DSeries;
				if (pointSeries == null)
					return;

				ChartScale hScale = dataView.HorizontalScale, vScale = dataView.VerticalScale;
				if (hScale == null || !hScale.CompatibleWith(dataView.ItemData.GetDimensionType(0))
					|| vScale == null || !vScale.CompatibleWith(dataView.ItemData.GetDimensionType(1)))
					return;

				// Point marker drawing.
				IPointMarker iPointMarker = dataView as IPointMarker;
				Debug.Assert(iPointMarker != null, "iPointMarker != null");
				Drawing pointMarkerDrawing = iPointMarker.PointMarker;
				if (!iPointMarker.PointMarkerVisible || pointMarkerDrawing == null)
					return;

				// Chart area size.
				Size areaSize = new Size(hScale.ToPixels(hScale.Stop), vScale.ToPixels(vScale.Stop));

				// Loop by points
				foreach (var pt in pointSeries.Points)
				{
					double x, y;
					if (dataView.Orientation == Orientation.Horizontal)
					{
						x = hScale.ToPixels(pt.X);
						y = vScale.ToPixels(pt.Y);
						if (!isInsideArea(new Point(x, y), areaSize))
							continue;
					}
					else // Orientation == Orientation.Vertical
					{
						y = hScale.ToPixels(pt.X);
						x = vScale.ToPixels(pt.Y);
						if (!isInsideArea(new Point(y, x), areaSize))
							continue;
					}

					Drawing marker = pointMarkerDrawing.Clone();
					marker.Freeze();

					PointVisual pointMarker = new PointVisual(marker);
					pointMarker.Transform = new TranslateTransform(x, y);
					Children.Add(pointMarker);
				}
			}
		}
	}
}
